//
//  CityRecorderInfoController.m
//  AppFrame
//
//  Created by GXJ on 2017/3/6.
//  Copyright © 2017年 GXJ. All rights reserved.
//

#import "CityRecorderInfoController.h"
#import "CityRecorderInfoCell.h"
#import "CityRecorderInfoSecCell.h"
#import "cityNewsObj.h"
#import "DTWebViewController.h"
#import "SharePublicServerView.h"
#import "CityRecorderObj.h"
@interface CityRecorderInfoController ()<SharePublicServerViewDelegate>
@property(nonatomic, strong)SharePublicServerView *shareView;

@end

@implementation CityRecorderInfoController
{
    NSArray *infoArr;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.showsVerticalScrollIndicator = NO;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
    {
        self.tableView.separatorInset = UIEdgeInsetsMake(0, SCREENWIDTH, 0, SCREENWIDTH);
    }
    
    [self requestCityNewsData];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    cityNewsObj *obj = infoArr[indexPath.row];
    DTWebViewController *webViewController = [[DTWebViewController alloc]init];
    webViewController.webViewUrlString = obj.url;
    webViewController.title = NSLocalString(@"新闻详情");
    [self.navigationController pushViewController:webViewController animated:YES];
    
    UIImage *image = [[UIImage imageNamed:@"分享举报"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    UIBarButtonItem *rightbuttonItem =  [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(rightFun)];
    webViewController.navigationItem.rightBarButtonItem = rightbuttonItem;
    
    
}

- (void)rightFun {
    //self.shareView = [[SharePublicServerView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)] ;
    //self.shareView.delegate = self;
    //UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    //[window addSubview:self.shareView];
    
    [SVProgressHUD showInfoWithStatus:@"敬请期待"];
}

#pragma mark --SharePublicServerView代理方法
- (void) SharePublicServerViewClickCancelBtn {
}

- (void) selectPublicServerItem:(NSInteger)index {
    NSString *str = [NSString stringWithFormat:@"%ld",index];
    [SVProgressHUD showWithStatus:str];
}

- (void) SharePublicServerViewHiddenView {
    self.shareView.hidden = YES;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return infoArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityRecorderInfoCell *cell = [CityRecorderInfoCell cellWithTableView:tableView];
    cityNewsObj *obj = infoArr[indexPath.row];
    cell.titleLab.text = obj.title;
    cell.nameLab.text = obj.excerpter;
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:obj.pic1]];
    cell.timeLab.text = obj.publishTime;
    return cell;
    
//    if(indexPath.section == 0) {
//        CityRecorderInfoCell *cell = [CityRecorderInfoCell cellWithTableView:tableView];
//        return cell;
//    }
//    else {
//        CityRecorderInfoSecCell *cell = [CityRecorderInfoSecCell cellWithTableView:tableView];
//        return cell;
//    }
}

/** 暂时不要分区  **/
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 32)];
//    view.backgroundColor = [UIColor whiteColor];
//    
//    if(section == 0)
//    {
//        UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(15, 12, 56, 16)];
//        [view addSubview:picture];
//        picture.image = [UIImage imageNamed:@"home recommendation"];
//    }
//    else
//    {
//        UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(15, 8, 56, 16)];
//        [view addSubview:picture];
//        picture.image = [UIImage imageNamed:@"livelihood recommendation"];
//        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 31, SCREENWIDTH, 1)];
//        line.backgroundColor = RGB(234, 234, 234);
//        [view addSubview:line];
//        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(SCREENWIDTH-15-16, 8, 16, 16)];
//        [button setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
//        [button addTarget:self action:@selector(delet) forControlEvents:UIControlEventTouchUpInside];
//        [view addSubview:button];
//    }
//    return view;
//}

-(void)delet {
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if(section == 0)
//        return 28;
//    else
//        return 32;
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 2.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0)
        return 20+(SCREENWIDTH-45)/3/11*8+20;
    else
        return 300;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //NSLog(@"%@",_indexNum);
    [self requestCityNewsData];
}


- (void) requestCityNewsData {
    //NSLog(@"-------%@",_indexNum);
    __weak __typeof(self) weakSelf = self;
    [DDNetWorkManager cityNewsWithCategoryId:_indexNum Controller:weakSelf success:^(id result, DDcacheType cacheType, DDresultType resultType) {
        [DDHudHelper showHudWithResult:result resultSuccess:^{
            NSLog(@"%@",result[@"data"]);
            infoArr = [cityNewsObj objectArrayWithKeyValuesArray:result[@"data"]];
            [self.tableView reloadData];
        }];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:NSLocalString(@"网络错误")];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
